/**
* project3D Engine
* @author John Sword
* @version 2 - AS3
*/

package engine.utils
{
	
	import flash.events.Event;
	import flash.text.TextField;
	import flash.utils.getTimer;
	
	public class FPS extends TextField
	{
		
		public var fps:Number = 0;
		
		private var update_delay:Number = 250;
   	 	private var starttime:Number = 0;
   	 	private var ceil:Function = Math.ceil;
   	 	private var frames:Number = 0;
		
		public function FPS ()
		{
			selectable = false;
			width = 20;
			height = 20;
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		public function onEnterFrame ( e:Event ) : void
		{
			frames ++;
			var currenttime:Number = getTimer();
		    if (starttime + update_delay < currenttime)
		    {
		        fps = ceil(Math.round(frames / ((currenttime - starttime) / 1000) * 1000) / 1000);
		        frames = 0;
		        text = fps.toString();
		        starttime = currenttime;
		    }
		}
		
	}
}